package com.flyan.game.motafx.core.equip;

import cn.hutool.core.util.ReflectUtil;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.entity.Entity;
import com.flyan.game.motafx.component.LifePanelComponent;
import com.flyan.game.motafx.core.enums.EquipTypeEnum;
import com.flyan.game.motafx.core.util.entity.EntityUtils;
import com.flyan.game.motafx.effect.BaseEffect;
import com.flyan.game.motafx.entity.item.ItemEntityInfo;
import lombok.Data;

/**
 * 装备
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/2/6
 */
@Data
public class Equip {

    /**
     * 装备实体
     */
    private final Entity entity;

    /**
     * 装备名称
     */
    private String name;

    /**
     * 装备类型
     */
    private EquipTypeEnum type;

    /**
     * 装备面板
     */
    private EquipPanel equipPanel;

    public Equip(Entity entity) {
        this.entity = entity;
        initEquipPanel();
    }

    private void initEquipPanel() {
        ItemEntityInfo info = EntityUtils.getEntityInfo(entity, ItemEntityInfo.class);
        this.name = info.getName();
        this.type = info.getObject("type", EquipTypeEnum.class);
        this.equipPanel = info.getObject("equipPanel", EquipPanel.class);
    }

    /**
     * 装备生效
     *
     * @param entity 生效在什么实体上
     */
    public void effect(Entity entity) {
        /* 更新生命面板 */
        if (entity.hasComponent(LifePanelComponent.class)) {
            LifePanelComponent lifePanelComponent = entity.getComponent(LifePanelComponent.class);
            lifePanelComponent.getLifePanel().increaseAck(equipPanel.getAck());
            lifePanelComponent.getLifePanel().increaseDef(equipPanel.getDef());
            lifePanelComponent.update();
        }
        /* 附加能力 */
        if (entity.hasComponent(EffectComponent.class)) {
            equipPanel.getAbilities().forEach(it -> {
                BaseEffect effect = ReflectUtil.newInstance(it);
                effect.setTriggerEntity(this.entity);
                effect.apply(entity);
            });
        }
    }

}
